#!/usr/bin/python

import sys,os,mysql,string

#take all db files in current one directory (zipped and unzipped)
# clean up header
# replace stereo_id with id
# cat into new db




stereo_table = sys.argv[1]  
stereo_id_field = sys.argv[2]

files_per_chunks = int(sys.argv[3])
db_prefix = sys.argv[4]


files = os.listdir('./')
files.sort()
counter = 0
pre_fix_counter = 0


def cat_files(db_prefix, pre_fix_counter):
	command = 'cat header *cor > ' + db_prefix + '_' + str(pre_fix_counter) + '.db'
	print command
	os.system(command)
	command = 'rm -f *cor'
	print command
	os.system(command)

	command = 'bzip2 ' + db_prefix + '_' + str(pre_fix_counter) + '.db'
	print command
	os.system(command)
	
	return


#-------------------------------------
def store_molis(molis,label, stereo_table, stereo_id_field,correct_db):

	#get id
	command = 'select id from ' + stereo_table + ' where ' + stereo_id_field + ' = ' + label
	#print command
	cursor.execute(command)
	results = cursor.fetchall()
	id = str(results[0][0])
	

	molis = molis.replace('xxxxxxxx', id)
	#print molis

	correct_db.write(molis)




#-------------------------------------

conn=mysql.connect2server('', 'webuser','purchasable') 
cursor = conn.cursor()

for file in files:
  if file[-3:] == '.db' or file[-4:] == '.bz2': #only do something if db file or zipped db file

	if file[-4:] == '.bz2':
		command = 'bunzip2 ' + file
		print command
		os.system(command)
		file = file[0:-4] 

	print 'clean up db file'
	#replace id numbers
	org_db = open(file, 'r')
	correct_db = open(file + '_cor', 'w')
	#first three lines contain header, delete
	for i in range(0,4):
		org_db.readline()
	

	#new line is family line, append to molecule
	family = True
	fam_line = org_db.readline()
	#print fam_line
	molecule = [fam_line]
	molis = ''
	first = True #this flagg is needed to make sure that the first molecule per file does not get lost
	wrong_line = False
	for j in org_db.xreadlines():
    		if family:
			label = string.strip(j[47:57])
			j = j.replace(label, 'xxxxxxxx')
			label = label[:-2] #last two digest are for multiple ring alignments
      			family = False

		   	if not j[0:3] == 'MMF': #there is something woring with the label line, don't load late
				wrong_line = True 

			if first:
				old_label = label
				first = False 
			if label <> old_label:
			   	if not wrong_line:
					store_molis(molis,label, stereo_table, stereo_id_field,correct_db)
				molis = ''
				old_label = label
				wrong_line = False


				
     		if j[0:6] =='Family':
       			family = True
			for k in molecule:
   				#print k
				molis = molis + k
			molecule = []
 
     		if j.find('0     0     0     0') == -1:
			#these lines would make dock stop
			molecule.append(j)
	#dont forget the last molcule
	for k in molecule:
		#print k
		molis = molis + k
 	#print molis
	store_molis(molis,label, stereo_table, stereo_id_field,correct_db)

	correct_db.close()
	org_db.close()

	#keep org db, but zip it again
	command = 'bzip2 ' + file
	print command
	os.system(command)

	print counter, files_per_chunks

	if counter == files_per_chunks:
		counter = 0
		cat_files(db_prefix, pre_fix_counter)
		pre_fix_counter = pre_fix_counter + 1

  	counter = counter + 1

cat_files(db_prefix, pre_fix_counter)

conn.close ()
